const axios = require("axios");
const moment = require("moment");
const util = require("util");
const logger = require("./logger");
const rds = require("./rds");
const tutils = require("./tutils");
const dbq = require("./dbq");
const task = require("./task");
const loyalty = require("./loyalty");
const ai = require("./ai");

async function test1(arg) {
    const token = 'w90aY-l_tg9PksdQ16AcculknAOAgE_Jh9NDHEYISDTjluOTR0R9xAL2UUEbko5D';
    const ret = await tutils.queryFollowTrace(token);
    console.log(ret);
}

async function test1() {
    const ret = task.assignTask(
        "uid", //通知对象识别形式，固定值
        1, //通知对象的标识，来自
        "trial_entry_event",
        "根据用户U#1上传的入组通知书为其确认分组",
        "Wework",
        "https://totemlife.cn/apps/trial/admin/trials/1/subjects/3", //这里的1是临床试验号,3是用户的受试者编号
        "用户U#1在试验#1进行了报名登记", //第一个1是提交了知情同意书的用户的uid,第个1是临床试验号
        "pending"
    )
    console.log(ret);
}

async function test1() {
    const ret = task.assignTask(
        "uid", //通知对象识别形式，固定值
        26061, //通知对象的标识，来自
        "trial_entry_event",
        "根据用户U#1上传的入组通知书为其确认分组",
        "Xiaoteng",
        "https://totemlife.cn/apps/trial/admin/trials/1/subjects/3", //这里的1是临床试验号,3是用户的受试者编号
        "用户U#1在试验#1进行了报名登记", //第一个1是提交了知情同意书的用户的uid,第个1是临床试验号
        "pending"
    )
    console.log(ret);
}

async function test1() {
        // 获取研究助理二维码
        let assistantQR = '/apps/public/avatar-cc-300x300.jpg';
        const [[assistant]] = await rds.query( 'SELECT qr_code FROM trial_assistants WHERE is_active = 1 ORDER BY id DESC LIMIT 1');
        if (assistant) assistantQR = assistant.qr_code;
        console.log(assistant);
}

async function sendSmsCode(phone, code) {
    try {
        const response = await axios.post('https://totemlife.cn/apps/cgi-bin/send-sms', {
            phone: phone,
            templateCode: 'SMS_206655517',  //图腾生命
            //templateCode: 'SMS_229760105', //图腾医学
            templateParam: {
                code: code
            }
        });

        console.log('发送结果:', response.data);
        return response.data;
    } catch (error) {
        console.error('发送失败:', error.response ? error.response.data : error.message);
        return { errcode: 1, errmsg: '发送失败' };
    }
}

// 生成4-6位随机验证码
function generateCode() {
    const length = Math.floor(Math.random() * 3) + 4; // 4-6位
    let code = '';
    for (let i = 0; i < length; i++) {
        code += Math.floor(Math.random() * 10);
    }
    return code;
}

async function sendVerifyCode(phone, code) {
    const response = await axios.get(`https://totemlife.cn/apps/cgi-bin/send-verify-code?phone=${phone}&code=${code}`);
    console.log('发送结果:', response.data);
    return response.data;

}

// 测试发送
async function test() {
    const phone = '13600060044'; // 替换为实际手机号
    const code = generateCode();
    console.log('生成的验证码:', code);
    //await sendSmsCode(phone, code);
    await sendVerifyCode(phone, code);
}

// 运行测试
test();
